import request from '../utils/request';

/**
 * 创建设备
 * @param {string} typeUuid 设备类型Uuid
 * @param {string} name 设备名称
 * @param {string} serialNumber 序列号/SN码
 * @param {string} description 设备描述
 * @param {number} package_id 升级包id
 * @param {number} setting_id 设置id
 */
export function createUpgradeDeviceApi(
  typeUuid,
  name,
  serialNumber,
  description,
  packageId,
  settingId
) {
  return request({
    url: '/products',
    method: 'post',
    data: {
      type_uuid: typeUuid,
      name: name || null,
      serial_number: serialNumber,
      description: description || null,
      package_id: packageId || null,
      setting_id: settingId || null
    }
  });
}

/**
 * 删除设备
 * @param {string} uuid
 */
export function deleteUpgradeDeviceApi(uuid) {
  return request({
    url: '/products/' + uuid,
    method: 'delete'
  });
}

export function deleteSelectedProductApi(uuidList) {
  return request({
    url: '/products/selected',
    method: 'delete',
    data: {
      uuid_list: uuidList
    }
  });
}

export function deleteProductListApi(
  productUuid,
  packageId,
  settingId
) {
  return request({
    url: '/products',
    method: 'delete',
    data: {
      product_uuid: productUuid,
      package_id: packageId || null,
      setting_id: settingId || null
    }
  });
}

/**
 * 更新设备
 * @param {string} uuid path
 * @param {string} typeUuid
 * @param {string} name 设备名称
 * @param {string} serialNumber 序列号
 * @param {string} description 设备描述
 * @param {number} packageId 升级包id
 * @param {number} settingId 设置id
 */
export function updateUpgradeDeviceApi(
  uuid,
  typeUuid,
  name,
  serialNumber,
  description,
  packageId,
  settingId
) {
  return request({
    url: '/products/' + uuid,
    method: 'put',
    data: {
      type_uuid: typeUuid,
      name: name || null,
      serial_number: serialNumber,
      description: description || null,
      package_id: packageId || null,
      setting_id: settingId || null
    }
  });
}

/**
 * 获取设备列表
 * @param {number} pageIndex 页码下标，默认从0开始
 * @param {number} pageSize 每页大小
 * @param {string} sortType 排序
 * @param {string} typeUuid 设备类型Uuid
 * @param {string} name 设备名称
 * @param {string} productNo 用户设备编号
 * @param {string} serialNumber 序列号
 * @param {string} imsi 国际移动用户识别码
 * @param {string} location 产品位置
 * @param {string} description 备注
 * @param {string} upgradeStatus 升级状态
 * @param {number} packageId 升级包id
 * @param {number} settingId 设备参数设置id
 */
export function getUpgradeDeviceListApi(
  pageIndex,
  pageSize,
  sortType,
  orderBy,
  typeUuid,
  name,
  productNo,
  serialNumber,
  imsi,
  location,
  description,
  upgradeStatus,
  packageId,
  settingId
) {
  return request({
    url: '/products',
    method: 'get',
    params: Object.assign(
      {
        page_index: pageIndex,
        page_size: pageSize,
        sort_type: sortType
      },
      orderBy ? { order_by: orderBy } : null,
      typeUuid ? { type_uuid: typeUuid } : null,
      name ? { name } : null,
      productNo ? { product_no: productNo } : null,
      serialNumber ? { serial_number: serialNumber } : null,
      imsi ? { imsi } : null,
      location ? { location } : null,
      description ? { description } : null,
      upgradeStatus ? { upgrade_status: upgradeStatus } : null,
      packageId ? { package_id: packageId } : null,
      settingId ? { setting_id: settingId } : null
    )
  });
}

/**
 * 批量更新设备
 * @param {array} productUuidList 产品uuid列表
 * @param {string} productTypeUuid 产品类型uuid
 * @param {number} packageId 升级包id
 * @param {number} settingId 设备设置id
 */
export function batchUpdateUpgradeDeviceApi(
  productUuidList,
  productTypeUuid,
  packageId,
  settingId
) {
  return request({
    url: '/products/batch',
    method: 'put',
    data: Object.assign(
      {
        product_uuid_list: productUuidList,
        product_type_uuid: productTypeUuid
      },
      packageId ? { package_id: packageId } : null,
      settingId ? { setting_id: settingId } : null
    )
  });
}

/**
 * 批量导入产品
 * @param {[string]} productUuidList 
 * @param {number} packageId 
 * @param {number} settingId 
 */
export function importProductListApi(
  productUuidList,
  packageId,
  settingId
) {
  return request({
    url: '/products/import',
    method: 'put',
    data: Object.assign(
      {
        product_uuid_list: productUuidList
      },
      packageId ? { package_id: packageId } : null,
      settingId ? { setting_id: settingId } : null
    )
  });
}

/**
 * 将设备从升级包或设备设置中移除
 * @param {string} productUuid
 * @param {number} packageId
 * @param {number} settingId
 */
export function deleteTagUpgradeDeviceApi(productUuid, packageId, settingId) {
  return request({
    url: '/products',
    method: 'delete',
    data: Object.assign(
      {
        product_uuid: productUuid
      },
      packageId ? { package_id: packageId } : null,
      settingId ? { setting_id: settingId } : null
    )
  });
}

/**
 * 获取产品列表信息
 * @param {number} pageIndex 
 * @param {number} pageSize 
 * @param {string} sortBy 
 * @param {string} orderBy 
 * @param {string} name 
 * @param {string} serialNumber 
 * @param {string} imsi 
 * @param {string} productNo 
 * @param {string} description 
 * @param {number} packageId 
 * @param {number} settingId 
 */
export function getChooseUpgradeDeviceList(pageIndex, pageSize, sortType, orderBy, name, serialNumber, imsi, productNo, description, packageId, settingId) {
  return request({
    url: '/products/list',
    method: 'get',
    params: Object.assign(
      {
        page_index: pageIndex,
        page_size: pageSize,
        sort_type: sortType,
        order_by: orderBy
      },
      name ? { name } : null,
      serialNumber ? { serial_number: serialNumber } : null,
      imsi ? { imsi } : imsi,
      productNo ? { product_no: productNo } : null,
      description ? { description } : null,
      packageId ? { package_id: packageId } : null,
      settingId ? { setting_id: settingId } : null
    )
  });
}

/**
 * 按时间范围获取重置产品信息
 * @param {string} uuid uuid
 * @param {number} startTime 
 * @param {number} endTime 
 */
export function getResetCountListRangeApi(serialNumber, startTime, endTime) {
  const baseUrl = (process.env.VUE_APP_BASE_API).split('/admin')[0];
  return request({
    url: baseUrl + '/dashboard/products/reset',
    method: 'get',
    params: {
      serial_number: serialNumber,
      start_time: startTime,
      end_time: endTime
    }
  });
}

/**
 * 查询产品
 * @param {string} name 
 * @param {number} take 
 */
export function queryProductListApi(name, take) {
  return request({
    url: '/products/query',
    method: 'get',
    params: {
      name, take
    }
  });
}

/**
 * 首页通过产品序列号查询产品接口
 * @param {string} name 序列号
 * @param {number} take 
 */
export function queryDashboardProductListApi(name, take) {
  const baseUrl = (process.env.VUE_APP_BASE_API).split('/admin')[0];
  return request({
    url: baseUrl + '/dashboard/products/query',
    method: 'get',
    params: {name, take}
  });
}

/**
 * 按产品参数升级状态分类获取产品数量
 */
export function getProductSettingStatusCountListApi() {
  const baseUrl = (process.env.VUE_APP_BASE_API).split('/admin')[0];
  return request({
    url: baseUrl + '/dashboard/products/setting',
    method: 'get'
  });
}

/**
 * 按固件包升级状态获取产品数量
 */
export function getProductPackageStatusCountListApi() {
  const baseUrl = (process.env.VUE_APP_BASE_API).split('/admin')[0];
  return request({
    url: baseUrl + '/dashboard/products/package',
    method: 'get'
  });
}

/**
 * 按产品分类获取产品数量
 * @param {number} take
 */
export function getProductTypeCountListApi(take) {
  const baseUrl = (process.env.VUE_APP_BASE_API).split('/admin')[0];
  return request({
    url: baseUrl + '/dashboard/products/type',
    method: 'get',
    params: {take}
  });
}
